<?php
/*
 *  FireFly theme
 *  (c) 2011, chugylo
 */

function get_theme_settings() {
    // parse ini-file for user settings
    $firefly_settings = parse_ini_file(get_template_directory() .'/firefly.ini');
    return $firefly_settings;
}

function firefly_init() {
    if (!is_admin()) {
        $settings = get_theme_settings();
        $jquery_cdn = $settings['jquery_cdn'];
        wp_deregister_script('jquery');
        wp_register_script('jquery', $jquery_cdn, array(), '1.6');  // jQuery from external CDN
        // main script
        if (debug())
            wp_register_script('firefly-script', get_template_directory_uri() .'/script.js', array('jquery'), '2');
        else  // use minified version
            wp_register_script('firefly-script', get_template_directory_uri() .'/script.min.js', array('jquery'), '2');
        wp_enqueue_script('jquery');
        wp_enqueue_script('firefly-script');
        wp_localize_script('firefly-script', 'objectL10n', array(
            'search' => __('Search', 'firefly')
        ));

        if (debug())
            wp_register_style('firefly-style', get_template_directory_uri() .'/style.css', array(), '3', 'all');
        else  // use minified version
            wp_register_style('firefly-style', get_template_directory_uri() .'/style.min.css', array(), '3', 'all');
        wp_enqueue_style('firefly-style');
    }
}

function most_commented_list($num=7) {
    // print list of most commented posts
    global $wpdb;
    $sql =
        // FIXME: is realy Wordpress generates URL from post_date or maybe from post_date_gmt etc?
        "SELECT post_date, post_title, post_name, comment_count
        FROM $wpdb->posts
        ORDER BY comment_count DESC
        LIMIT $num";
    $result = $wpdb->get_results($sql);
    $array = array();
    foreach ($result as $r) {
        $date = explode(' ', $r->post_date);
        $date = $date[0];
        $year = substr($date, 0, 4);
        $month = substr($date, 5, 2);
        $day = substr($date, 8, 2);
        $url = site_url() ."/$year/$month/$day/$r->post_name/";
        $array[] = array($r->post_title, $url, $r->comment_count);
    }
    foreach ($array as $a)
        // $a[0] - title
        // $a[1] - URL
        // $a[2] - comment count
        echo "    <li><a href=\"$a[1]\">$a[0]</a></li>\n";
}

function firefly_tags($input, $separator=', ') {
    // Extended the_tags() function.
    // It consider get_the_tags() output and prints "No tags", "Tag: " or "Tags: ".
    // Common usage: firefly_tags(get_the_tags());
    $number = count($input);
    $title = __('Get all publications tagged as %s', 'firefly');
    if ($number > 1) {
        echo __('Tags: ', 'firefly');
        $array = array();
        foreach ($input as $tag_obj)
            array_push($array, array($tag_obj->name, get_tag_link($tag_obj->term_id)));
        $htmled_tags = array();
        foreach ($array as $a)
            array_push($htmled_tags, sprintf("<a href=\"%s\" title=\"$title\" rel=\"tag\">%s</a>", $a[1], $a[0], $a[0]));
        echo implode($separator, $htmled_tags);
        echo "\n";
    }
    elseif ($number == 1 && $input !== false) {
        echo __('Tag: ', 'firefly') ;
        $tag = current($input);  // index may not be equal to 0 so we get value in such way
            printf("<a href=\"%s\" title=\"$title\" rel=\"tag\">%s</a>\n", get_tag_link($tag->term_id), $tag->name, $tag->name);
    }
    else
        _e('No tags', 'firefly');
}

function firefly_categories($input, $separator=', ') {
    // Extended the_category() function.
    // It consider get_the_category() output and prints "Category: " or "Categories: ".
    // Common usage: firefly_categories(get_the_category());
    $number = count($input);
    $title = __('Get all publications in %s', 'firefly');
    if ($number > 1) {
        echo __('Categories: ', 'firefly');
        $array = array();
        foreach ($input as $cat_obj)
            array_push($array, array($cat_obj->cat_name, get_category_link($cat_obj->cat_ID)));
        $htmled_cats = array();
        foreach ($array as $a)
            array_push($htmled_cats, sprintf("<a href=\"%s\" title=\"$title\" rel=\"category tag\">%s</a>", $a[1], $a[0], $a[0]));
        echo implode($separator, $htmled_cats);
        echo "\n";
    }
    elseif ($number == 1 && $input !== false) {
        echo __('Category: ', 'firefly') ;
        $cat = current($input);  // index may not be equal to 0 so we get value in such way
            printf("<a href=\"%s\" title=\"$title\" rel=\"category tag\">%s</a>\n", get_category_link($cat->cat_ID), $cat->cat_name, $cat->cat_name);
    }
}

function use_in_head_insertion() {
    // return boolean value
    $settings = get_theme_settings();
    if ($settings['in_head_insertion'])
        return true;
    else
        return false;
}
function use_sidebar_boxes_insertion() {
    // return boolean value
    $settings = get_theme_settings();
    if ($settings['sidebar_boxes_insertion'])
        return true;
    else
        return false;
}

function show_author() {
    // show or do not show post's author
    // return boolean
    $settings = get_theme_settings();
    return $settings['show_author'];
}

function debug () {
    $settings = get_theme_settings();
    return $settings['debug'];
}

function copyright_years() {
    // Print years for copyright notice. It gets foundation year from
    // theme settings and minds about current year.
    $current_year = date('Y');
    $settings = get_theme_settings();
    if ($current_year == $settings['foundation_year'])
        echo $current_year;
    else
        echo "${settings['foundation_year']}–$current_year";
}

function copyright_name() {
    // print copyright name
    $settings = get_theme_settings();
    echo $settings['copyright_name'];
}

function firefly_version() {
    // print FireFly version (letter "v" and numbers)
    $settings = get_theme_settings();
    echo 'v'. $settings['firefly_version'];
}

load_theme_textdomain('firefly', get_template_directory() .'/languages');
add_action('init', 'firefly_init');

?>